<script setup lang="ts">
import {reactive, ref} from "vue";
import {User, Lock} from '@element-plus/icons-vue'
import {login} from "@/netForAxios/net";
import router from "@/router";

const form = reactive({
  username: '',
  password: '',
  remember: false,
})
const rule = {
  username: [
    {
      required: true, message: '请输入用户名'
    }
  ],
  password: [
    {
      required: true, message: '请输入密码'
    }
  ]
}
const formCheck = ref()

function userLogin() {
  formCheck.value.validate((valid) => {
    if (valid) {
      login(form.username, form.password, form.remember, () => {
        router.push('/home')
      })
    }
  })
}
</script>

<template>
  <div class="login-box">
    <h1>Login</h1>
    <div class="login-form">
      <el-form :model="form" :rules="rule" ref="formCheck">
        <el-form-item prop="username">
          <el-input v-model="form.username" maxlength="20" type="text" placeholder="用户名"
                    style="height: 40px;text-indent: 4px">
            <template #prefix>
              <el-icon>
                <User/>
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="form.password" maxlength="20" type="password" placeholder="密码"
                    style="height: 40px;text-indent: 4px;">
            <template #prefix>
              <el-icon>
                <Lock/>
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
      </el-form>
      <el-row style="margin-top: 30px" gutter="5">
        <el-col :span="12" style="text-align: left">
          <el-button @click="userLogin" style="width: 100%;height: 40px" type="primary" plain>登入</el-button>
        </el-col>
        <el-col :span="12" style="text-align: right">
          <el-button @click="router.push('/register')" style="width: 100%;height: 40px" type="primary" plain>注册
          </el-button>
        </el-col>
      </el-row>
      <el-divider style="margin-top: 50px;"/>
      <span style="line-height: 15px;font-size: 14px;font-weight: bold;letter-spacing: 3px">已有账号?</span>
      <el-link style="translate: 0 -2px;font-weight: bold;letter-spacing: 3px" type="primary" @click="router.push('/reset')">忘记密码</el-link>
    </div>
  </div>
</template>

<style scoped>
.login-box h1 {
  margin-top: 130px;
  letter-spacing: 5px;
  text-shadow: 4px 4px 3px rgba(0, 0, 0, 0.1);
  color: white;
  text-align: center;
}

.login-form {
  width: 60%;
  margin: 50px auto;
  text-align: center;
}
</style>